package com.loong.mapper.impl;

import com.loong.mapper.ProductMapper;
import com.loong.pojo.Product;
import com.loong.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.List;

public class ProductMapperImpl implements ProductMapper {
    Logger logger = Logger.getLogger(ProductMapperImpl.class);

    @Override
    public long selectCountByTid(String tid) {
        try(SqlSession sqlSession = MybatisUtils.getSqlSession()){
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.selectCountByTid(tid);
        }catch (Exception e){
            logger.error(e.getMessage());
            return 0;
        }
    }

    @Override
    public List<Product> selectProductByPage(String tid,int page, int pageSize) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.selectProductByPage(tid, (page - 1) * pageSize, pageSize);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    @Override
    public Product selectProductByPid(int pid) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.selectProductByPid(pid);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }
}
